package com.jyxd.web.service.assessment.basic;

import com.jyxd.web.dao.basic.BloodSugarDao;
import com.jyxd.web.dao.dictionary.CommentItemDao;
import com.jyxd.web.data.basic.BloodSugar;
import com.jyxd.web.data.dto.BaseDataDto;
import com.jyxd.web.data.dto.BloodSugarDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

import static com.jyxd.web.util.UUIDUtil.getUUID;

@Service
@Transactional
public class BloodSugarService {

    @Autowired
    private BloodSugarDao bloodSugarDao;
    @Autowired
    private CommentItemDao commentItemDao;

    public boolean insert(BloodSugar bloodSugar) {
        return bloodSugarDao.insert(bloodSugar);
    }

    public boolean update(BloodSugar bloodSugar) {
        return bloodSugarDao.update(bloodSugar);
    }

    public boolean delete(BloodSugar bloodSugar) {
        return bloodSugarDao.delete(bloodSugar);
    }

    public void deleteByRelevancyId(String relevancyId) {
        BloodSugar bloodSugar = bloodSugarDao.queryByRelevancyId(relevancyId);
        if (Objects.isNull(bloodSugar))
            return;
        bloodSugar.setStatus(-1);
        delete(bloodSugar);
    }

    public BloodSugar queryData(String id) {
        return bloodSugarDao.queryData(id);
    }

    public List<BloodSugar> queryList(Map<String, Object> map) {
        return bloodSugarDao.queryList(map);
    }

    public int queryNum(Map<String, Object> map) {
        return bloodSugarDao.queryNum(map);
    }

    /**
     * 根据时间查询在科病人的血糖信息
     *
     * @param map
     * @return
     */
    public List<Map<String, Object>> getListByTime(Map<String, Object> map) {
        return bloodSugarDao.getListByTime(map);
    }

    /**
     * 根据病人id code 时间 查询血糖对象
     *
     * @param map
     * @return
     */
    public BloodSugar queryDataByCodeAndPatientId(Map<String, Object> map) {
        return bloodSugarDao.queryDataByCodeAndPatientId(map);
    }

    /**
     * 根据id查询血糖对象
     *
     * @param map
     * @return
     */
    public BloodSugar queryDataById(Map<String, Object> map) {
        return bloodSugarDao.queryDataById(map);
    }

    /**
     * 根据时间和病人id查询血糖列表
     *
     * @param map
     * @return
     */
    public List<BloodSugar> queryListByTimeAndPatientId(Map<String, Object> map) {
        return bloodSugarDao.queryListByTimeAndPatientId(map);
    }

    /**
     * 护理文书--血糖监测--根据病人id 和 时间查询血糖列表
     *
     * @param map
     * @return
     */
    public List<Map<String, Object>> getList(Map<String, Object> map) {
        return bloodSugarDao.getList(map);
    }

    /**
     * 批量添加
     *
     * @param listNew
     * @return
     */
    public boolean insertBatch(List<BloodSugar> listNew) {
        return bloodSugarDao.insertBatch(listNew);
    }

    /**
     * 根据rowId查询数据
     *
     * @param rowId
     */
    public List<BloodSugar> queryListByRowId(String rowId) {
        return bloodSugarDao.queryListByRowId(rowId);
    }

    /**
     * 查询某个病人的血糖数据
     *
     * @param patientId
     */
    public List<BloodSugar> queryDataNew(String patientId) {
        return bloodSugarDao.queryDataNew(patientId);
    }

    /**
     * 根据患者id，页码，和每页数据查询最新的数据
     *
     * @return
     */
    public Map<String, Object> queryDataByPageAndPatientId(Map<String, Object> map) {
        Map<String, Object> mapData = new HashMap<String, Object>();
        int start = 0;
        int size = Integer.parseInt(map.get("size").toString());
        int sum = bloodSugarDao.queryNumByPatientId(map.get("patientId").toString());
        int maxStart = sum / size + 1;
        if (map.get("start") == null) {
            start = maxStart;
        } else {
            start = Integer.parseInt(map.get("start").toString());
        }
        int pageSize = (start - 1) * size;
        map.put("start", pageSize);
        List<BloodSugarDTO> list = bloodSugarDao.queryDataByPageAndPatientId(map);
        if (list == null || list.size() == 0) {
            for (int i = 0; i < size; i++) {
                BloodSugarDTO bloodSugar = new BloodSugarDTO();
                bloodSugar.setId("");
                bloodSugar.setRowId("");
                bloodSugar.setRelevancyId("");
                bloodSugar.setContent("");
                bloodSugar.setCode("");
                bloodSugar.setPatientId("");
                bloodSugar.setDataTime("");
                list.add(bloodSugar);
            }
        } else {
            if (list.size() < 46) {
                int sumSize = size - list.size();
                for (int i = 0; i < sumSize; i++) {
                    BloodSugarDTO bloodSugar = new BloodSugarDTO();
                    bloodSugar.setId("");
                    bloodSugar.setRowId("");
                    bloodSugar.setRelevancyId("");
                    bloodSugar.setContent("");
                    bloodSugar.setCode("");
                    bloodSugar.setPatientId("");
                    bloodSugar.setDataTime("");
                    list.add(bloodSugar);
                }
            }

        }
        mapData.put("maxStart", maxStart);
        mapData.put("start", start);
        mapData.put("data", list);
        return mapData;
    }

    public boolean saveOrUpdate(BaseDataDto baseDataDto) {
        if (Objects.isNull(baseDataDto))
            return false;
        BloodSugar bloodSugar = bloodSugarDao.queryByRelevancyId(baseDataDto.getKeyDistinguishId());
        boolean isNewBloodSugar = false;
        if (Objects.isNull(bloodSugar)) {
            bloodSugar = new BloodSugar();
            bloodSugar.setId(getUUID());
            bloodSugar.setStatus(1);
            bloodSugar.setCreateTime(new Date());
            bloodSugar.setRelevancyId(baseDataDto.getKeyDistinguishId());
            isNewBloodSugar = true;
        }
        bloodSugar.setPatientId(baseDataDto.getOtherId());
        bloodSugar.setContent(baseDataDto.getContent());
        bloodSugar.setDataTime(baseDataDto.getTime());
        bloodSugar.setCode(baseDataDto.getSign());
        if (isNewBloodSugar)
            bloodSugarDao.insert(bloodSugar);
        else
            bloodSugarDao.update(bloodSugar);
        return true;
    }
}
